package com.tecent.chat.actvity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tecent.chat.R;
import com.tecent.chat.data.DatabaseHelper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class LoginActivity extends BaseActivity {

    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final EditText user = findViewById(R.id.username);
        final EditText password = findViewById(R.id.password);
        final Button btn = findViewById(R.id.login);
        final Button sql = findViewById(R.id.sql1);
        final Button sql2 = findViewById(R.id.sql2);
        final Button sql3 = findViewById(R.id.sql3);
        final Button sql4 = findViewById(R.id.sql4);

        dbHelper = new DatabaseHelper(this, "db-hua", null, 1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // btn.setEnabled(false);
                String u = user.getText().toString();
                String p = password.getText().toString();
                if (u.equals("admin") && p.equals("123")) {
                    Log.v("main", "ok");
                    Intent intent = new Intent(LoginActivity.this, BroadcastActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "error", Toast.LENGTH_SHORT).show();
                    get1();
                }
            }
        });

        // 多次点，不会重复提示，数据库创建成功，因为oncreate只会执行一次
        sql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回一个SQLiteDatabase对象，借助这个对象就可以对数据进行CRUD操作了。
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                ContentValues val = new ContentValues();
                val.put("id", 1);
                val.put("name", "nina");
                db.insert("user", null, val);
                val.clear();

                val.put("id", 2);
                val.put("name", "tina");
                db.insert("user", null, val);

                Toast.makeText(LoginActivity.this, "插入数据成功", Toast.LENGTH_SHORT).show();

            }
        });

        sql2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues val = new ContentValues();
                val.put("name", "sasa");
                db.update("user", val, "id = ?", new String[]{"1"});

                Toast.makeText(LoginActivity.this, "update数据成功", Toast.LENGTH_SHORT).show();
            }
        });

        sql3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("user", "id = ?", new String[]{"1"});

                Toast.makeText(LoginActivity.this, "删除数据成功", Toast.LENGTH_SHORT).show();
            }
        });

        sql4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                Cursor cursor = db.rawQuery("select * from user", null);


                if(cursor.moveToFirst()){
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        Log.v("main", name);
                    }while (cursor.moveToNext());
                }

                cursor.close();
                Toast.makeText(LoginActivity.this, "查询数据成功", Toast.LENGTH_SHORT).show();
            }
        });

    }

    // 将数据存储到SharedPreferences
    public void save1() {
        // getShared-Preferences()方法指定SharedPreferences的文件名为data,
        // 生产文件的路径为  /shared_prefs/data.xml
        // 并得到了SharedPreferences.Editor对象
        SharedPreferences.Editor editor = getSharedPreferences("data", 0).edit();
        editor.putString("name", "huahuadavids");
        editor.putInt("age", 34);
        editor.putBoolean("newbee", true);
        editor.apply();
        Toast.makeText(LoginActivity.this, "success!!", Toast.LENGTH_SHORT).show();
    }

    // 从SharedPreferences中取数据
    public void get1() {
        SharedPreferences share = getSharedPreferences("data", 0);
        String name = share.getString("name", "");
        Log.v("main", name);
    }

    // 保存数据到本地文件
    public void save() {
        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            out = openFileOutput("data", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write("huahuadavids");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                    Log.v("main", "write end");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 加载本地文件的数据
    public String load() {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();

        try {
            in = openFileInput("data");
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";

            while ((line = reader.readLine()) != null) {
                content.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Log.v("main", content.toString());

        return content.toString();
    }
}
